// filepath: /d:/aiWorkspace/video-generation/video-generation-app/src/api/axios-config.ts
import axios from 'axios';
import router from '../router';

// 创建 axios 实例
const instance = axios.create({
  baseURL: '/api',
  timeout: 10000
});

// 请求拦截器
instance.interceptors.request.use(
  config => {
    // 在请求头中添加 token
    const token = localStorage.getItem('token');
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截器
instance.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response && error.response.status === 401) {
      // Token is missing or invalid, redirect to login page
      router.push('/login');
    }
    return Promise.reject(error);
  }
);

export default instance;